/**
 * Copyright (c) [2025] [AGIROS] 
 * [travoddsgen] is licensed under Mulan PSL v2. 
 * You can use this software according to the terms and conditions of the Mulan PSL v2. 
 * You may obtain a copy of Mulan PSL v2 at: 
 *          http://license.coscl.org.cn/MulanPSL2 
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 
 * See the Mulan PSL v2 for more details.
 */ 

#include "CLangTypeSupportSourceVisitor.h"
#include "CppLangTypeSupportHeaderVisitor.h"
#include "CppLangTypeSupportSourceVisitor.h"

void CLangTypeSupportSourceVisitor::visit(SpecificationNode& node) {
    // 头文件包含
    std::string includes = "";
    includes += "#include \"" + headerFile_ + "\"\n";
    includes += "#include \"dcps/topic/typesupport.h\"\n";
    includes += "\n";
    content_ += includes;

    // namespace
    content_ += "USING_AGIDDS_NAMESPACE\n";
    content_ += "\n";

    // __cplusplus宏定义开始
    content_ += "#ifdef __cplusplus\n";
    content_ += "extern \"C\" {\n";
    content_ += "#endif\n\n";

    // 递归访问子节点
    if (node.definitions_) {
        node.definitions_->accept(*this);
    }

    // __cplusplus宏定义结尾
    content_ += "#ifdef __cplusplus\n";
    content_ += "}\n";
    content_ += "#endif\n";

    // 将_content写入文件
    writeToFile();
}

void CLangTypeSupportSourceVisitor::visit(StructDefNode& node) {
    // 调用c++对应的方法
    CppLangTypeSupportHeaderVisitor cppHeaderVisitor("", "", typeNameVisitor_, headerFile_);
    cppHeaderVisitor.visit(node);
    content_ += cppHeaderVisitor.getContent();

    CppLangTypeSupportSourceVisitor cppVisitor("", "", typeNameVisitor_, headerFile_);
    cppVisitor.visit(node);
    content_ += cppVisitor.getContent();

    // 针对每个结构体XXX生成，XXXTypeSupport_get_instance函数
    std::string structName = typeNameVisitor_->findName(&node);
    content_ += "void* " + structName + "TypeSupport_get_instance() {\n";
    content_ += "    return " + structName + "TypeSupport::get_instance()" + ";\n";
    content_ += "}\n";
    content_ += "\n";
}